package com.jaydenxiao.common.base;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.jaydenxiao.common.BR;
import com.jaydenxiao.common.drag_listener.ItemTouchHelperViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017/10/14.
 */

public class BaseBindingAdapter<D,B extends ViewDataBinding> extends RecyclerView.Adapter<BaseBindingVH<B>> {


    protected Context context;
    protected int layoutId;
    protected List<D> dataList;
    protected Object itemPresent;

    public BaseBindingAdapter(Context context) {
        this.context = context;
    }

    public BaseBindingAdapter(Context context, int layoutId, List<D> dataList) {
        this.context = context;
        this.layoutId = layoutId;
        this.dataList = dataList;
    }

    public BaseBindingAdapter(Context context,List<D> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public BaseBindingVH<B> onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseBindingVH<B> baseBindingVH= (BaseBindingVH<B>) new BaseBindingVH<>(
                DataBindingUtil.inflate(LayoutInflater.from(context),layoutId,parent,false));
        onCreateViewHolder(baseBindingVH);
        baseBindingVH.getmBinding().setVariable(BR.itemP,itemPresent);
        return baseBindingVH;
    }

    protected void onCreateViewHolder(BaseBindingVH<B> baseBindingVH) {

    }

    @Override
    public void onBindViewHolder(BaseBindingVH<B> holder, int position) {
        holder.setIndex(position);
        holder.getmBinding().setVariable(BR.data,dataList.get(position));
        holder.getmBinding().executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return dataList==null?0:dataList.size();
    }

    public Object getItemPresent() {
        return itemPresent;
    }

    public void setItemPresent(Object itemPresent) {
        this.itemPresent = itemPresent;
    }

    /**
     * 刷新数据，初始化数据
     *
     * @param list
     */

    public void setDatas(List<D> list) {
        if (this.dataList != null) {
            if (null != list) {
                List<D> temp = new ArrayList<D>();
                temp.addAll(list);
                this.dataList.clear();
                this.dataList.addAll(temp);
            } else {
                this.dataList.clear();
            }
        } else {
            this.dataList = list;
        }
        notifyDataSetChanged();
    }

    /**
     * 删除一条数据
     * 会自动定向刷新
     *
     * @param i
     */
    public void remove(int i) {
        if (null != dataList && dataList.size() > i && i > -1) {
            dataList.remove(i);
            notifyItemRemoved(i);
        }
    }

    public void remove(Object object) {
        if (null != dataList) {
            dataList.remove(object);
            notifyDataSetChanged();
        }
    }

    /**
     * 添加一条数据 至队尾
     * 会自动定向刷新
     * @param data
     */
    public void add(D data) {
        if (data != null && dataList != null) {
            dataList.add(data);
            notifyItemInserted(dataList.size());
        }
    }


    /**
     * 在指定位置添加一条数据
     * 会自动定向刷新
     * <p>
     * 如果指定位置越界，则添加在队尾
     *
     * @param position
     * @param data
     */
    public void add(int position, D data) {
        if (data != null && dataList != null) {
            if (dataList.size() > position && position > -1) {
                dataList.add(position, data);
                notifyItemInserted(position);
            } else {
                add(data);
            }
        }
    }

    /**
     * 加载更多数据
     *
     * @param list
     */
    public void addDatas(List<D> list) {
        if (null != list) {
            List<D> temp = new ArrayList<D>();
            temp.addAll(list);
            if (this.dataList != null) {
                this.dataList.addAll(temp);
            } else {
                this.dataList = temp;
            }
            notifyDataSetChanged();
        }
    }


    public List<D> getDatas() {
        return dataList;
    }


}
